/*
 *  Methods.cpp
 *  NiNJA
 *
 *  Created by waver on 09-11-12.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#import "Methods.h"


double PubMethod::powi(double v, int k)
{
	double temp = 1.0;
	if(k == 0||v == 0)
	{
		return 1;
	}
	else
	{
		for(int i=1;i<=k;i++)
		{
			temp = temp*v;
		}
	}
	return temp;
}


long PubMethod::fac(int m)
{
	int i = 0;
	long temp = 1;
	if(m == 0)
	{
		return 1;
	}
	else
	{
		for(i=2;i<=m;i++)
		{
			temp = temp*i;
		}
	}
	return temp;
}

int PubMethod::unsignInt(int a)
{
	if(a < 0)
	{
		return a * (-1);
	}
	else
	{
		return a;
	}
}

bool	PubMethod::IsInArea(CoordinateValue one_co,CoordinateValue two_co,int x_limit)
{
	int aim_space = one_co.x-two_co.x;
	
	aim_space = unsignInt(aim_space);
	
	if(aim_space <= x_limit)//在范围内
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool  PubMethod::LongInArea(int one_point,int two_point,int num_limit)
{
	int aim_space = one_point-two_point;
	
	aim_space = unsignInt(aim_space);
	
	if(aim_space <= num_limit)//在范围内
	{
		return true;
	}
	else
	{
		return false;
	}
}


void  PubMethod::Aleart(const char * message_dis)
{
	cout << message_dis << endl;
}


void PubMethod::RemoveIntVectorItem(vector<int> & select_index,int remove_item)
{
	typedef vector<int>::iterator int_it;
	
	int_it it_first = select_index.begin();
	
	for(it_first; it_first != select_index.end();it_first++)
	{
		if( (*it_first) == remove_item )
		{
			select_index.erase(it_first);
			
			return;
		}
	}
}

bool PubMethod::IsIntInVector(vector<int> select_index,int item)
{
	vector<int> ::iterator it_first;
	
	it_first = select_index.begin();
	
	while (it_first != select_index.end()) {
		
		if(*it_first == item)
		{
			return true;
		}
		
		it_first++;
	}
	return false;
}

int	PubMethod::NewRand(int time_add, int max_n_set)
{
	int rand_n = 0;
	for(int i = 0 ;i < 3;i++)
	{
		srand(time(0) + time_add);
		
		rand_n = rand() % max_n_set;
		
		time_add = rand_n;
	}
	
	return rand_n;
}

bool PubMethod::CheckPointInRectangle(CoordinateValue point_check, CoordinateValue left_up_point,CoordinateValue right_down_point)
{
	if(point_check.x<left_up_point.x || point_check.x > right_down_point.x ||point_check.y < left_up_point.y || point_check.y > right_down_point.y)
	{
		return false;
	}
	else
	{
		return true;
	}
}

int PubMethod::GetNumberBit(int num)
{
	int i=0;
	do 
	{
		i++;
	}while ((num /= 10) != 0);
	return i;
}

int PubMethod::GetBitNumber(int num,int bit_point)
{
	int n = GetNumberBit(num);
	
	if(bit_point > n)
	{
		return 0;
	}
	else if(num == 0)
	{
		return 0;
	}
	else if(bit_point == 0)
	{
		return 0;
	}
	else
	{
		int temp = num/(powi(10,bit_point - 1));
		
		if(temp < 10)
		{
			return temp;
		}
		else
		{
			return temp%10;
		}
	}
}

double PubMethod::CalculateAgleToRadian(int angle)
{
	double radiansResult = 0;
	
	radiansResult = ((double)angle)/180.0 * 3.1415927;
	
	return radiansResult;
}

string PubMethod::IntToString(int num)
{
	stringstream ss;
	
	ss<<num;
	
	string str = ss.str();
	
	if(!ss.good())
	{
		cout<<"int to string error!"<<endl;
	}
	
	return str;
}

float PubMethod::TwoPointAngle(float x1,float y1,float x2,float y2)
{
	float x,y,fHyp,cos,rad,deg;
    x = x2 - x1;
    y = y1 - y2;
	
    fHyp = sqrt(pow(x,2) + pow(y,2));
	
    cos = x / fHyp;
    rad = acos(cos);
	
    deg = 180/(M_PI / rad);
	
    if(y <0)
    {
        deg = -deg;
    }
    else if((y == 0) && (x <0))
    {
        deg = 180;
    }
	
    return deg;
}

int     PubMethod::NewPosition(int playerOrd, int ord)
{
    vector<int> ordVec;
    ordVec.clear();
    for(int i=0;i <4;i++)
    {
        ordVec.push_back(i);
    }
    vector<int> newOrdVec;
    newOrdVec.clear();
    for(int i = playerOrd;i >= 0;i--)
    {
        newOrdVec.push_back(i);
    }
    for(int i = 3; i>playerOrd;i--)
    {
        newOrdVec.push_back(i);
    }
    
    for(int i =0;i<4;i++)
    {
        if(newOrdVec[i] == ord)
        {
            return i;
        }
    }
    
    return 0;
    
}
